package direct;

import Constant.constant;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建链接工厂
        ConnectionFactory factory=new ConnectionFactory();
        //2.设置参数
        factory.setHost(constant.localhost);
        factory.setPort(constant.port);
        factory.setVirtualHost(constant.VirtuHost);
        factory.setUsername(constant.UserName);
        factory.setPassword(constant.Password);
        //3.创立链接
        Connection connection=factory.newConnection();
        //4.创建channel信道
        Channel channel=connection.createChannel();

        //声明交换机
        channel.exchangeDeclare(constant.DIRECT_EXCHANGE, BuiltinExchangeType.DIRECT,true);
        //声明队列
        channel.queueDeclare(constant.DIRECT_QUEUE2,true,false,false,null);
        DefaultConsumer consumer=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收到消息:"+new String(body));
            }
        };
        channel.basicConsume(constant.DIRECT_QUEUE2,true,consumer);

    }
}
